﻿using HCQ2WebAPI_Logic.BaseAPIController;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using HCQ2_Model.ViewModel.WebAPI;
using HCQ2_Common.Constant;
using HCQ2WebAPI_Logic.Model;
using HCQ2_Model.APPModel.ParamModel;
using HCQ2_Model;
using HCQ2_Model.APPModel.ResultApiModel;
using System.Globalization;
using HCQ2_Model.VR.Params;
using HCQ2_Model.DocModel.APIParam;
using HCQ2_Model.DocModel.APIResultModel;
using HCQ2_Common;
using HCQ2_Model.VR.Model;

namespace HCQ2WebAPI_Logic.DocumentController
{
    /// <summary>
    ///  体验用户
    /// </summary>
    //[HCQ2_Common.Attributes.SkipApi]
    public class ExperienceUserController: BaseWeiXinApiLogic
    {
        //**************************************************1.0 体验用户**********************************
        #region ✔1.0  添加体验记录 + async Task<WebApiResultJsonModel> AddRecordExperience(ExperienceRecord model)
        /// <summary>
        ///  添加体验记录
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel AddRecordExperience(ExperienceRecord model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            try
            {
                if(model.file_id<=0 && string.IsNullOrEmpty(model.file_code))
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception,"file_id、file_code不能同时不传！", null);
                //1.1 获取登录用户信息
                int userId;
                string userName = this.GetUserName(model.userid, out userId);
                if(userId<=0)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "体验用户数据不存在~", null);
                //1.2 验证课件
                T_DocumentInfo addFile;
                if (model.file_id > 0)
                    addFile = operateContext.bllSession.T_DocumentInfo.FindSingle(s => s.file_id == model.file_id && s.file_status == 5 && s.if_remove);
                else
                    addFile = GetFileStatusName(model.file_code);
                if (addFile == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, string.Format(" {0}：当前课件在已上架商品中不存在！", model.file_code), null);
                else
                    model.file_id = addFile.file_id;
                //1.3 验证关卡
                T_VRCustomPass addPass;
                if (!string.IsNullOrEmpty(model.folder_code))
                    addPass = getCustomPass(model.folder_code);
                else
                    addPass = operateContext.bllSession.T_VRCustomPass.FindSingle(s => s.file_id == model.file_id && s.cp_name.Equals(model.folder_name));
                if (addPass == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "关卡异常！", null);
                else
                    model.folder_name = addPass.cp_name;
                model.user_name = userName;
                model.user_id = userId;
                model.file_name = addFile.file_name;
                model.in_date = DateTime.Now.ToString("yyyyMMddHHmmss");
                if (string.IsNullOrEmpty(model.folder_code))
                    model.folder_code = "";
                if (string.IsNullOrEmpty(model.file_code))
                    model.file_code = "";
                if (string.IsNullOrEmpty(model.em_time))
                    model.em_time = "0";
                T_VRExperienceRecord reord = model.ToEntity();
                int mark = operateContext.bllSession.T_VRExperienceRecord.Add(reord);
                if (mark > 0)
                {
                    #region VRLines添加体验主表
                    if (addFile.finish_time > 0)
                    {
                        string tempName = operateContext.bllSession.T_VRExperienceMain.GetFolderName(reord.file_id);
                        ExperienceMainResult mainResult = operateContext.bllSession.T_VRExperienceMain.GetSchoolInfo(reord.user_id);
                        T_VRExperienceMain obj = new T_VRExperienceMain
                        {
                            user_id = reord.user_id,
                            user_name = reord.user_name,
                            file_id = reord.file_id,
                            file_name = reord.file_name,
                            finsh_len = addFile.finish_time,
                            learn_len = Helper.ToInt(reord.em_time),
                            out_time = reord.out_date ?? DateTime.Now,
                            type_text = tempName,
                            type_value = tempName,
                            schoolCode = mainResult.schoolCode,
                            schoolName = mainResult.schoolName,
                            className = mainResult.className
                        };
                        var query = operateContext.bllSession.T_VRExperienceMain.FindSingle(s => s.user_id == reord.user_id && s.file_id == reord.file_id);
                        if (query != null)
                        {
                            obj.learn_len += query.learn_len;
                            //更新
                            operateContext.bllSession.T_VRExperienceMain.Modify(obj, s => s.file_id == reord.file_id && s.user_id == reord.user_id, "learn_len", "out_time");
                        }
                        else
                            //新增
                            operateContext.bllSession.T_VRExperienceMain.Add(obj);
                    }
                    #endregion
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "体验记录添加成功~", reord.er_id);
                }
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "体验记录添加失败~", "");
            }
            catch(Exception ex)
            {
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, ex.Message, "");
            }
        }
        #endregion

        #region ✔1.0.2  添加体验明细记录 + async Task<WebApiResultJsonModel> AddDetialExperience(ExperienceDetialList model)
        /// <summary>
        ///  添加体验明细记录
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel AddDetialExperience(ExperienceDetialList model)
        {
            try
            {
                if (!ModelState.IsValid)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
                int user_id = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
                if (user_id <= 0)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "体验用户身份异常~", null);
                StringBuilder sb = new StringBuilder();//异常记录
                //1.0 验证
                foreach (ExperienceDetial item in model.record)
                {
                    if (!CheckExperience(item.er_id))
                    {
                        sb.AppendFormat("{0}：体验主表记录不存在！", item.er_id);
                        break;
                    }
                }
                if (!string.IsNullOrEmpty(sb.ToString()))
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, sb.ToString(), null);
                T_VRExperienceRecord record = operateContext.bllSession.T_VRExperienceRecord.FindSingle(s => s.er_id == model.record[0].er_id);
                //2.0 添加记录
                foreach (ExperienceDetial item in model.record)
                {
                    //主表记录
                    item.user_id = user_id;
                    T_VRExperienceDetail detail = item.ToEntity();
                    operateContext.bllSession.T_VRExperienceDetail.Add(detail);
                    //2.2 添加错误记录
                    if (item.ed_status == 0)
                    {
                        T_VRErrorWord word = operateContext.bllSession.T_VRErrorWord.Select(s => s.user_id == user_id && s.word.Equals(item.word) && s.if_remove == false && s.file_code.Equals(record.file_code)  && s.cp_code.Equals(record.folder_code)).FirstOrDefault();
                        if (null != word)
                            operateContext.bllSession.T_VRErrorWord.Modify(new T_VRErrorWord { create_date = DateTime.Now, err_num = word.err_num + 1 }, s => s.ws_id == word.ws_id, "create_date", "err_num");
                        else
                            operateContext.bllSession.T_VRErrorWord.Add(new T_VRErrorWord { user_id = user_id, err_num = 1, word = item.word, if_remove = false, create_date = DateTime.Now, file_code = record.file_code, cp_code = record.folder_code });
                    }
                }
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "体验明细添加成功~", "");
            }
            catch(Exception ex)
            {
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, ex.Message, null);
            }
        }
        #endregion

        #region ✔1.0.3  移出错误单词标记 + async Task<WebApiResultJsonModel> MarkErrorWord(ErrorWordModel model)
        /// <summary>
        ///  移出错误单词标记
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel MarkErrorWord(ErrorWordModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            if(model.word==null ||model.word.Count<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "没有需要移除的是记录~", null);
            int countLen = 0;
            foreach (string word in model.word)
            {
                int mark = operateContext.bllSession.T_VRErrorWord.SelectCount(s => s.user_id == user_id && s.word.Equals(word, StringComparison.OrdinalIgnoreCase));
                if (mark <= 0)
                    continue;
                operateContext.bllSession.T_VRErrorWord.Modify(new T_VRErrorWord { if_remove = true, create_date = DateTime.Now }, s => s.user_id == user_id && s.word.Equals(word, StringComparison.OrdinalIgnoreCase), "if_remove", "create_date");
                countLen++;
            }
            if (countLen > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, string.Format("错词移出标记添加成功，成功移除{0}条记录~", countLen), "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "错词移出标记添加失败~", "");
        }
        #endregion

        #region ✔1.1  获取体验记录 + async Task<WebApiResultJsonModel> GetRecordList(QueryRecordModel model)
        /// <summary>
        ///  获取体验记录
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetRecordList(QueryRecordModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            List<ExperienceRecord> list = operateContext.bllSession.T_VRExperienceRecord.GetRecordListByUser(model, userId);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取体验记录成功~", list);
        }
        #endregion

        #region 2.0  添加当前位置 + async Task<WebApiResultJsonModel> AddCurrentPoint(CurrentPointModel model)
        /// <summary>
        ///  添加当前位置
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel AddCurrentPoint(CurrentPointModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            //1.0 判断素材/作品是否存在
            string fileName = GetFileName(model.file_id);
            if (string.IsNullOrEmpty(fileName))
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "体验的作品不存在~", null);
            //1.1 获取登录用户信息
            int userId;
            string userName = this.GetUserName(model.userid, out userId);
            model.user_id = userId;
            model.user_name = userName;
            model.file_name = fileName;
            //1.2 判断同一个人，同一个作品体验位置是否存在
            T_VRCurrentPoint point = operateContext.bllSession.T_VRCurrentPoint.Select(s => s.user_id == userId && s.file_id == model.file_id && s.folder_name.Equals(model.folder_name)).FirstOrDefault();
            int mark;
            if (point == null)
                mark = operateContext.bllSession.T_VRCurrentPoint.Add(model.ToEntity());
            else
                mark = operateContext.bllSession.T_VRCurrentPoint.Modify(model.ToEntity(), s => s.cp_id == point.cp_id, "out_date", "x_place", "y_place", "z_place");
            if (mark > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "当前位置表添加成功~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "当前位置表添加失败~", "");
        }
        #endregion

        #region 2.1  获取当前位置列表 + async Task<WebApiResultJsonModel> GetRecordList(QueryRecordModel model)
        /// <summary>
        ///  获取当前位置列表
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetCurrentPointByUser(QueryRecordModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            CurrentPointModel point = operateContext.bllSession.T_VRCurrentPoint.Select(s => s.user_id == userId && s.file_id == model.file_id && s.folder_name.Equals(model.folder_name))?.Select(s=>s.ToPOCO()).FirstOrDefault();
            if(point==null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "当前位置记录为空~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取当前位置成功~", point);
        }
        #endregion

        #region 3.0  添加在线用户 + async Task<WebApiResultJsonModel> AddOnLineUser(OnLineUserModel model)
        /// <summary>
        ///  添加在线用户
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel AddOnLineUser(OnLineUserModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            //1.0 判断素材/作品是否存在
            string fileName = GetFileName(model.file_id);
            if (string.IsNullOrEmpty(fileName))
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "体验的作品不存在~", null);
            //1.1 获取登录用户信息
            int userId;
            string userName = this.GetUserName(model.userid, out userId);
            model.user_id = userId;
            model.user_name = userName;
            model.file_name = fileName;
            //1.2 在线状态
            if (model.line_status.Equals("online"))
                model.line_status = "在线";
            else
                model.line_status = "离线";
            //判断记录是否存在：保证一个人，同一个项目只有一条记录
            T_VROnLineUser line = operateContext.bllSession.T_VROnLineUser.Select(s => s.user_id == model.user_id && s.file_id == model.file_id && s.folder_name.Equals(model.folder_name)).FirstOrDefault();
            int mark;
            if (line != null)
                mark = operateContext.bllSession.T_VROnLineUser.Modify(new T_VROnLineUser { in_date = DateTime.ParseExact(model.in_date, "yyyyMMddHHmmss", CultureInfo.CurrentCulture, DateTimeStyles.None), line_status = model.line_status }, s => s.on_id == line.on_id, "in_date", "line_status");
            else
                mark = operateContext.bllSession.T_VROnLineUser.Add(model.ToEntity());
            if (mark > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "在线用户添加成功~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "在线用户添加失败~", "");
        }
        #endregion

        #region 3.1  获取在线用户记录 + async Task<WebApiResultJsonModel> GetOnLineUser(QueryRecordModel model)
        /// <summary>
        ///  获取在线用户记录
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetOnLineUser(QueryRecordModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            List<OnLineUserModel> list = operateContext.bllSession.T_VROnLineUser.GetOnLineUser(model);
            if(list==null || list.Count<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "根据条件未查询到在线用户记录~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取体验记录成功~", list);
        }
        #endregion

        #region 4.0  添加体验排行 + async Task<WebApiResultJsonModel> AddExperienceRank(ExperienceRankModel model)
        /// <summary>
        ///  添加体验排行
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel AddExperienceRank(ExperienceRankModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            //1.0 判断素材/作品是否存在
            string fileName = GetFileName(model.file_id);
            if (string.IsNullOrEmpty(fileName))
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "体验的作品不存在~", null);
            //1.1 获取登录用户信息
            int userId;
            string userName = this.GetUserName(model.userid, out userId);
            model.user_id = userId;
            model.user_name = userName;
            model.file_name = fileName;
            int mark = operateContext.bllSession.T_VRExperienceRank.Add(model.ToEntity());
            if (mark > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "体验排行添加成功~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "体验排行添加失败~", "");
        }
        #endregion

        #region 4.1  获取体验排行列表 + async Task<WebApiResultJsonModel> GetPopularityRank(QueryRecordModel model)
        /// <summary>
        ///  获取体验排行列表
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetPopularityRank(QueryRecordModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            if(string.IsNullOrEmpty(model.file_code))
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "课件代码不能为空！", null);
            model.file_id = 0;
            if(string.IsNullOrEmpty(model.folder_code))
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "关卡代码数据不存在！", null);
            var pass = getCustomPass(model.folder_code);
            if(pass==null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "关卡代码数据不存在！", null);
            if (string.IsNullOrEmpty(model.folder_name))
                model.folder_name = pass.cp_name;
            List<ExperienceRankModel> list = operateContext.bllSession.T_VRExperienceRecord.GetPopularityRankList(model);
            if (list == null || list.Count <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "根据条件未查询到体验排行记录~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取体验排行记录成功~", list);
        }
        #endregion

        #region 5.0  添加个人成绩 + async Task<WebApiResultJsonModel> AddScoreByUser(VRScoreModel model)
        /// <summary>
        ///  添加个人成绩
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel AddScoreByUser(VRScoreModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            //1.0 判断素材/作品是否存在
            string fileName = GetFileName(model.file_id);
            if (string.IsNullOrEmpty(fileName))
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "体验的作品不存在~", null);
            //1.1 获取登录用户信息
            int userId;
            string userName = this.GetUserName(model.userid, out userId);
            model.user_id = userId;
            model.user_name = userName;
            model.file_name = fileName;
            int mark = operateContext.bllSession.T_VRScore.Add(model.ToEntity());
            if (mark > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "个人成绩添加成功~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "个人成绩添加失败~", "");
        }
        #endregion

        #region 5.1  获取个人成绩列表 + async Task<WebApiResultJsonModel> GetScoreByUser(QueryRecordModel model)
        /// <summary>
        ///  获取个人成绩列表
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetScoreByUser(QueryRecordModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            List<VRScoreModel> list = operateContext.bllSession.T_VRScore.GetScoreListByUser(model, userId);
            if (list == null || list.Count <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "根据条件未查询到个人成绩记录~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取个人成绩记录成功~", list);
        }
        #endregion

        //************************************************** 6.0 班级管理 *************************************************
        #region ✔6.1  添加我关注的班级 + async Task<WebApiResultJsonModel> AddOwnFavoritesClass(FavoritesClassModel model)
        /// <summary>
        ///  添加我关注的班级
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel AddOwnFavoritesClass(FavoritesClassModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            //1.0 判断我关注的班级是否存在
            int mark = operateContext.bllSession.T_Class.SelectCount(s => s.id == model.class_id);
            if(mark<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "当前班级已删除或者不存在~", null);
            int user_id = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            model.user_id = user_id;
            //1.1 判断当前关注班级记录是否已存在
            T_VRFocusClass result = operateContext.bllSession.T_VRFocusClass.Select(s => s.class_id == model.class_id && s.user_id == user_id).FirstOrDefault();
            bool isMark = model.mark > 0 ? true : false;
            if (result!=null && result.mark== isMark)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "已完成操作~", "");
            if (result != null)
                mark = operateContext.bllSession.T_VRFocusClass.Modify(new T_VRFocusClass { mark = isMark }, s => s.fc_id == result.fc_id, "mark");
            else
                 mark = operateContext.bllSession.T_VRFocusClass.Add(model.ToEntity());
            if (mark > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "操作成功~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "操作失败~", "");
        }
        #endregion

        #region ✔6.2  获取班级列表 + async Task<WebApiResultJsonModel> GetClassList(ClassListModel model)
        /// <summary>
        ///  获取班级列表
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetClassList(ClassListModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            List<ClassListResult> list = operateContext.bllSession.T_Class.GetClassList(model, userId);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取班级列表成功~", list);
        }
        #endregion

        #region ✔6.3  获取关注班级列表 + async Task<WebApiResultJsonModel> GetFocusClassList(ClassListModel model)
        /// <summary>
        ///  获取关注班级列表
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetFocusClassList(ClassListModel model)
        {
            if (model.markStatus < 1)
                model.markStatus = 0;
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            List<ClassListResult> list = operateContext.bllSession.T_Class.GetFocusClassList(model, userId);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取关注班级列表成功~", list);
        }
        #endregion

        //************************************************** 7.0 体验排行*************************************************
        #region ✔7.0  获取体验时长排行榜 + async Task<WebApiResultJsonModel> GetExperienceRank(ExperienceRankParam model)
        /// <summary>
        ///  获取体验时长排行榜
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetExperienceRank(ExperienceRankParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            List<ExperienceRankList> list = operateContext.bllSession.T_Class.GetExperienceRankList(model, userId);
            if (list == null || list.Count <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "根据条件未查询到体验排行榜记录~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取体验排行记录成功~", list);
        }
        #endregion

        #region ✔7.1  获取体验正确率统计 + async Task<WebApiResultJsonModel> GetCorrectRate(ExperienceRankParam model)
        /// <summary>
        ///  获取体验正确率统计
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetCorrectRate(ExperienceRankParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            List<CorrectRateResult> list = operateContext.bllSession.T_Class.GetCorrectRateList(model, userId);
            if (list == null || list.Count <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "根据条件未查询到体验排行榜记录~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取体验排行记录成功~", list);
        }
        #endregion

        #region ✔7.2  获取单词出错次数统计 + async Task<WebApiResultJsonModel> GetWordErrorNum(ExperienceRankParam model)
        /// <summary>
        ///  获取单词出错次数统计
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetWordErrorNum(ExperienceRankParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            List<WordErrorModel> list = operateContext.bllSession.T_Class.GetWordErrorNumList(model, userId);
            if (list == null || list.Count <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "根据条件未查询到单词体验错误记录~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取单词错误次数成功~", list);
        }
        #endregion

        #region ✔7.3  获取用户登录信息 + async Task<WebApiResultJsonModel> GetUserInfoByLogin(ExperienceRankParam model)
        /// <summary>
        ///  获取用户登录信息
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetUserInfoByLogin(ExperienceRankParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            if (model.userType > 0)
                model.unit_code = operateContext.bllSession.T_User.FindSingle(s => s.user_id == userId)?.unit_code;
            List<UserInfoByLoginModel> list = operateContext.bllSession.T_Class.GetUserLoginInfo(model, userId);
            if (list == null || list.Count <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "根据条件未查询到用户登录信息~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取用户登录信息成功~", list);
        }
        #endregion

        #region ✔7.4  获取学生列表 + async Task<WebApiResultJsonModel> GetStudentInfo(StudentLoginModel model)
        /// <summary>
        ///  获取学生列表
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetStudentInfo(StudentLoginModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            List<StudentInfoResult> list = operateContext.bllSession.T_Class.GetStudentInfoList(model, userId);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "获取学生信息成功~", list);
        }
        #endregion

        #region ✔7.5  获取在线体验用户数量 + async Task<WebApiResultJsonModel> GetStudentInfo(StudentLoginModel model)
        /// <summary>
        ///  获取在线体验用户数量
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetOnLineNum(StudentLoginModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            int mark= operateContext.bllSession.T_Class.GetOnLineNum(model, userId);
            if (mark<= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "根据查询条件未统计出体验人数~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功统计出在线体验人员~", mark);
        }
        #endregion

        #region ✔7.6  获取关卡解锁标记 + async Task<WebApiResultJsonModel> GetPassMark(NextPassParam model)
        /// <summary>
        ///  获取关卡解锁标记
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetPassMark(NextPassParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int userId = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(model.userid);
            //1.0 课件
            T_DocumentInfo info = operateContext.bllSession.T_DocumentInfo.Select(s => s.file_code.Equals(model.file_code) && s.file_status == 5).FirstOrDefault();
            if(info==null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "课件代码异常 或者 此课件已下架！", null);
            //2.0 关卡
            T_VRCustomPass pass = operateContext.bllSession.T_VRCustomPass.Select(s => s.file_id == info.file_id && s.cp_code.Equals(model.folder_code)).FirstOrDefault();
            if(pass==null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "关卡代码数据不存在！", null);
            decimal? oldScore = operateContext.bllSession.T_VRExperienceRecord.Select(s => s.user_id == userId && s.file_id == info.file_id && s.folder_code.Equals(pass.cp_code)).Max(s => s.score);
            decimal score = 0;//返回分数
            NextPassStartModel startModel = new NextPassStartModel();
            if (oldScore != null)
            {
                startModel.score = (decimal)oldScore;
                int temp = Convert.ToInt32(startModel.score) / 20;
                if (temp <= 0)
                    startModel.start = 0;
                else if (temp > 5)
                    startModel.start = 5;
                else
                    startModel.start = temp;
                if (Convert.ToInt32(startModel.score) >= 60)
                    startModel.status = true;
            }
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取关卡解锁标记！", startModel);
        }
        #endregion

        /// <summary>
        ///  获取登录用户信息
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        private string GetUserName(string guid,out int userId)
        {
            T_User user = operateContext.bllSession.T_User.FindSingle(s => s.user_guid.Equals(guid));
            userId = 0;
            if (user == null)
                return string.Empty;
            userId = user.user_id;
            return user.user_name;
        }
        /// <summary>
        ///  获取体验作品名称
        /// </summary>
        /// <param name="fileId"></param>
        /// <returns></returns>
        private string GetFileName(int fileId)
        {
            T_DocumentInfo file = operateContext.bllSession.T_DocumentInfo.Select(s => s.file_id.Equals(fileId))?.FirstOrDefault();
            if (file == null)
                return string.Empty;
            return file.file_name;
        }
        /// <summary>
        ///  根据code获取已上架商品名
        /// </summary>
        /// <param name="fileCode"></param>
        /// <returns></returns>
        private T_DocumentInfo GetFileStatusName(string fileCode)
        {
            return operateContext.bllSession.T_DocumentInfo.FindSingle(s => s.file_code.Equals(fileCode) && s.file_status == 5);
        }
        /// <summary>
        ///  验证体验记录主表是否存在
        /// </summary>
        /// <param name="erId"></param>
        /// <returns></returns>
        private bool CheckExperience(int erId)
        {
            int mark = operateContext.bllSession.T_VRExperienceRecord.SelectCount(s => s.er_id == erId);
            return mark > 0 ? true : false;
        }
        /// <summary>
        ///  验证体验详细 单词 是否在后台存在
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        private bool CheckErrorWord(string word)
        {
            int count = operateContext.bllSession.T_VRCustomPassWord.SelectCount(s => s.word.Equals(word, StringComparison.CurrentCultureIgnoreCase));
            return count > 0 ? true : false;
        }
    }
}
